﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLKS.Templates;
using System.Data;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmThemKhachHang.xaml
    /// </summary>
    public partial class frmThemKhachHang : FormTrans
    {
        public bool UpdateSucces = false;
        Command cm;
        DataTable Data = new DataTable();
        public frmThemKhachHang()
        {
            InitializeComponent();
        }

        public frmThemKhachHang(DataTable tb, Command _cm)
        {
            InitializeComponent();
            IsEditMode = _cm == Command.View ? false : true;
            cm = _cm;
            cbbLoaiKH.ItemsSource = ViewModels.KhachHangViewModel.GetNhomKhachHang().DefaultView;
            if (tb == null)
                Data = ViewModels.KhachHangViewModel.CreateNewKhachHang();
            else
                Data = tb.Copy();
            if (cm == Command.Copy)
            {
                Data.Rows[0]["makh"] = ViewModels.KhachHangViewModel.GetMaKhMoi();
                Data.Rows[0]["gioitinh"] = "Nam";
            }
            gridMain.DataContext = Data.DefaultView; 
        }

        #region 'PROPERTIES'

        //IsEditMode
        public bool IsEditMode
        {
            get { return (bool)GetValue(IsEditModeProperty); }
            set { SetValue(IsEditModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditModeProperty =
            DependencyProperty.Register("IsEditMode", typeof(bool), typeof(frmThemKhachHang), new UIPropertyMetadata(true));
        #endregion


        private void FrmThemKhachHang_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsEditMode)
            {
                tbMain.Cm_Save += new ucXToolBar.Execute(tbMain_Cm_Save);
                tbMain.Cm_New +=new ucXToolBar.Execute(tbMain_Cm_New);
            }
            txtTenKH.Focus();
            tbMain.Cm_Exit += new ucXToolBar.Execute(tbMain_Cm_Exit);
        }

        //Kiểm tra dữ liệu hợp lệ
        private bool KiemTra()
        {
            if (string.IsNullOrEmpty(txtMaKH.Text))
            {
                MessageBox.Show(M_LAN.Equals("V")?"Chưa nhập mã":"Input Id, please!", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtMaKH.Focus();
                return false;
            }
            else
                if (string.IsNullOrEmpty(txtTenKH.Text))
                {
                    MessageBox.Show(M_LAN.Equals("V")?"Chưa nhập họ tên":"Input name, please!", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtTenKH.Focus();
                    return false;
                }
                else
                    if (string.IsNullOrEmpty(txtCMND.Text))
                    {
                        MessageBox.Show(M_LAN.Equals("V") ? "Chưa nhập CMND" : "Input identity card,Please!", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtCMND.Focus();
                        return false;
                    }
                    else
                        if (string.IsNullOrEmpty(cbbLoaiKH.Text))
                        {
                            MessageBox.Show(M_LAN.Equals("V")?"Chưa chọn loại KH":"Select customer type, please!", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                            cbbLoaiKH.Focus();
                            return false;
                        }
                        else
                            if (DateTime.Now.Year <= int.Parse(txtNamSinh.Value.ToString()))
                            {
                                MessageBox.Show(M_LAN.Equals("V") ? "Năm sinh phải nhỏ hơn hoặc bằng năm hiện tại!" : "Year of birth must be less than or equal to the current year!", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                                txtNamSinh.Focus();
                                return false;
                            }

            return true;
        }

        private void tbMain_Cm_Save()
        {
            //Thêm vào CSDL dữ liệu
            if (KiemTra())
            {
                if (ViewModels.KhachHangViewModel.ThemKhachHang(Data))
                {
                    this.UpdateSucces = true;
                    if (cm == Command.Edit)
                        MessageBox.Show(M_LAN.Equals("V")?"Cập nhật nhân viên thành công thành công!":"Update staff success!", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                    else
                    {
                        MessageBox.Show(M_LAN.Equals("V") ? "Thêm nhân viên thành công thành công!" : "Added staff success!", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                        cm = Command.Edit;
                    }
                        
                
                }
            }
        }

        //Tao moi
        public void tbMain_Cm_New()
        {
            Data = ViewModels.KhachHangViewModel.CreateNewKhachHang();
            gridMain.DataContext = Data.DefaultView;
            cm = Command.New;
            txtTenKH.Focus();
        }

        //Đóng form
        public void tbMain_Cm_Exit()
        {
            this.Close();
        }

        private void cbbLoaiKH_GotFocus(object sender, RoutedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb != null)
                cb.IsDropDownOpen = true;
        }

        private void txtNamSinh_GotFocus(object sender, RoutedEventArgs e)
        {
            txtNamSinh.SelectAll();
        }

        private void txtGhiChu_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            Data.Rows[0]["ghichu"] = txtGhiChu.Text;
        }
    }
}
